package com.jd.basic.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传:写
 * 删除:写
 * 修改:不写
 * 查询:不写
 */

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping
    public AjaxResult upload(@RequestPart MultipartFile file){
        try {
            // 获取原始文件
            String oName = file.getOriginalFilename();
            String[] arr = oName.split("\\.");

            String fileName = FastDfsUtils.upload(file.getBytes(), arr[arr.length - 1]);

            return AjaxResult.me().setResultObj(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }
    }


    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path){

        try {

            String path1 = path.substring(1);
            String para1 = path1.substring(0, path1.indexOf("/"));
            String para2 = path1.substring(path1.indexOf("/") + 1);
            FastDfsUtils.delete(para1,para2);
            return new AjaxResult();

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }


    }



}
